package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"patient-care/models"
)

type Patient struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (p *Patient) List() {

	page, _ := p.GetInt("page", 1)
	size, _ := p.GetInt("size", 3)
	offset := (page - 1) * size
	query := models.DB.Model(&models.Patient{})
	var count int64
	query.Count(&count)
	var patient []models.Patient
	err := query.Offset(offset).Limit(size).Find(&patient).Error
	if err != nil {
		p.Data["json"] = Res{
			Code: 0,
			Msg:  "查询失败",
		}
		p.ServeJSON()
		return
	} else {
		PageData := make(map[string]interface{})
		PageData["count"] = count
		PageData["patient"] = patient
		p.Data["json"] = Res{
			Code: 200,
			Msg:  "查询成功",
			Data: PageData,
		}
		p.ServeJSON()
	}

}
